<?php

Class IOSS_StaticConfig
{
    // 加密解密秘钥
    public static $iv = '1234567891234567';
    public static $encryptKey = 'ytk1234567890000';

    //小程序基本参数
    public static $AppId = 'wx22ea83bfa9e5fa19';
    public static $Secret = '7260b1de24211d8ffe4cb52541058936';
    public static $GrantType = 'authorization_code';
    public static $AuthUrl = 'https://api.weixin.qq.com/sns/jscode2session';
    public static $MchId = '1605151486';
    public static $PayKey = '1234567890LxzcFuYangLiao33854616';
    public static $norMalPrice = 0.50;

    public static $orderType = Array(
        0 => '<label class="label label-default">未还车</label>',
        1 => '<label class="label label-info">申请还车中</label>',
        2 => '<label class="label label-success">确认已还车</label>',
        3 => '<label class="label label-danger">故障报修</label>'
    );

    public static function getOrderType($method = '')
    {
        if ($method === '') {
            return self::$orderType;
        }
        return element($method, self::$orderType, '');
    }

    public static function receive_notify($notify){
        $db = IOSS_DB::getInstance();
        $exist = $db->select( '*')
            ->from('pay_notify')
            ->where('out_trade_no',$notify['out_trade_no'])
            ->get()
            ->row();
        if (!$exist){
            $insert = [
                'result_code' => $notify['result_code'],
                'err_code' => $notify['err_code'],
                'err_code_des' => $notify['err_code_des'],
                'openid' => $notify['openid'],
                'trade_type' => $notify['trade_type'],
                'total_fee' => $notify['total_fee'],
                'fee_type' => $notify['fee_type'],
                'cash_fee' => $notify['cash_fee'],
                'transaction_id' => $notify['transaction_id'],
                'out_trade_no' => $notify['out_trade_no'],
                'time_end' => $notify['time_end'],
                'bank_type' => $notify['bank_type'],
            ];
            $insert_id = $db->insert('pay_notify',$insert);
            return $insert_id;
        }
        return false;
    }

    public static function check_order($orderNo, $fee){
        $db = IOSS_DB::getInstance();
        $pay_order = $db->select("*")
            ->from('wx_pay')
            ->where('out_trade_no',$orderNo)
            ->get()
            ->row();
        // 验证金额
        if ((float)$pay_order->total_fee !== (float)$fee){
            return false;
        }

        //租车订单
        if ($pay_order->oid){
            if ($db->update('order',['pay_status' => 1,'pay_time' => time()],['id' => $pay_order->oid]) && $db->update('wx_pay',['status' => 1],['id' => $pay_order->id])){
                return true;
            }else{
                return false;
            }
        //缴纳保证金
        }else if ($pay_order->pid){
            if ($db->update('user_promise',['status' => 1,'pay_time' => time()],['id' => $pay_order->pid]) && $db->update('wx_pay',['status' => 1],['id' => $pay_order->id]) && $db->update('user',['promise_status' => 1],['id' => $pay_order->user_id])){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    /**
     * 获取最近七天所有日期
     */
    public static function get_weeks($time = '', $format='Y-m-d',$days = 7, $type = 'days'){
        $time = $time != '' ? $time : time();
        //组合数据
        $date = [];
        for ($i = 0; $i <= $days -1 ;$i++ ){
            $date[$i] = date($format ,strtotime( '+' . $i-$days .' '.$type, $time));
        }
        return $date;
    }

    public static function get_yesterday(){
        $y = date("Y", time()); //年
        $m = date("m", time()); //月
        $d = date("d", time()); //日
        $t0 = date('t'); // 本月一共有几天
        $r=array();
        $r['start'] = mktime(0, 0, 0, $m, $d-1, $y); // 创建本月开始时间
        $r['end'] = mktime(23, 59, 59, $m, $d-1, $y); // 创建本月结束时间
        return $r;
    }

    public static function get_b_and_e(){
        $timestr = time();
        $now_day = date('w',$timestr);
        //获取一周的第一天，注意第一天应该是星期天
        $sunday_str = $timestr - $now_day*60*60*24;
        $sunday = date('Y-m-d', $sunday_str);
        //获取一周的最后一天，注意最后一天是星期六
        $strday_str = $timestr + (6-$now_day)*60*60*24;
        $strday = date('Y-m-d', $strday_str);
        return [strtotime($sunday),strtotime($strday.' 23:59:59')];
    }

    public static function get_this_month(){
        $y = date("Y", time()); //年
        $m = date("m", time()); //月
        $d = date("d", time()); //日
        $t0 = date('t'); // 本月一共有几天
        $r=array();
        $r['start_month'] = mktime(0, 0, 0, $m, 1, $y); // 创建本月开始时间
        $r['end_month'] = mktime(23, 59, 59, $m, $t0, $y); // 创建本月结束时间
        return $r;
    }

    /**
     * 转换成 年 天 时 分 秒
     * @param [type] $time
     */
    public static function Sec2Time($time)
    {
        if (is_numeric($time)) {
            $value = array(
                "years" => 0, "days" => 0, "hours" => 0,
                "minutes" => 0, "seconds" => 0,
            );
            $t = '';
            if ($time >= 31556926) {
                $value["years"] = floor($time / 31556926);
                $time = ($time % 31556926);
                $t .= $value["years"] . "年";
            }
            if ($time >= 86400) {
                $value["days"] = floor($time / 86400);
                $time = ($time % 86400);
                $t .= $value["days"] . "天";
            }
            if ($time >= 3600) {
                $value["hours"] = floor($time / 3600);
                $time = ($time % 3600);
                $t .= $value["hours"] . "小时";
            }
//            if ($time >= 60) {
//                $value["minutes"] = floor($time / 60);
//                $time = ($time % 60);
//                $t .= $value["minutes"] . "分";
//            }
//            $value["seconds"] = floor($time);
//            //return (array) $value;
//            $t .= $value["seconds"] . "秒";
            return $t;

        } else {
            return false;
        }
    }
}
